package com.nchu.web.interceptor;

import javax.servlet.http.HttpSession;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
//import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.nchu.web.model.ProblemsHelper;
import com.nchu.web.model.Quiz;

public class ApplicationInterceptor implements Interceptor{

	@Override
	public void intercept(Invocation inv) {
			
	    Controller controller =inv.getController();	   
	    HttpSession session=controller.getSession();
	    String s=(String)session.getAttribute("teacher_user");
	    /**
	    // String str =controller.getSessionAttr("Tproblem");
	    HttpServletRequest request =controller.getRequest();
	    Cookie[] cookies=request.getCookies();
	    String prob = null;
	    for(Cookie cookie : cookies){	    	
	    	System.out.println("name:"+cookie.getName());
	    	System.out.println("value:"+cookie.getValue());
	    }
	    */
	    ProblemsHelper ph = ProblemsHelper.dao.findByTeacherName(s);
	    String content ="";
		if(ph==null){
			
		}else{
			content = ph.getTProblem();
		}	
	    String appointStu ="";
	    Quiz mq = Quiz.dao.findByTeacherName(s);
	    if(mq==null){
	    	
	    }else{
	        appointStu =mq.getStuId();
	    }
	   
	    if((s!="")&&(s!=null)){
			controller.setAttr("tea_id", s);
			controller.setAttr("application",JFinal.me().getServletContext()).setAttr("problem",content);
			/**application --javax.servlet.ServletContext*/
			controller.setAttr("application",JFinal.me().getServletContext()).setAttr("appointSID",appointStu);	
			inv.invoke();
	    }else{
			controller.forwardAction("/visit"); 
		}
	}

	
}
